package inabit.inabitcompiler.productions.fuzzy;

import inabit.compiler.parser.grammar.NonTerminal;
import inabit.compiler.parser.grammar.Production;
import inabit.compiler.parser.grammar.Symbol;
import inabit.compiler.parser.grammar.Terminal;
import inabit.fuzzy.data.FuzzySet;
import inabit.fuzzy.data.GaussianFunction;
import inabit.inabitcompiler.lexical.InabitTokenClass;
import inabit.inabitcompiler.parser.InabitSemanticAction;

/**Fs' -> define [BELLSHAPED(N; N) FROM N TO N] AS id;Fs''*/
/**@author Rafael Rocha*/
public class Fs1_Rule2 extends Production{
	private NonTerminal tkMean, tkDp, tkStart, tkEnd;
	private Terminal tkId;
	public Fs1_Rule2() {
		super("Fs'");
	}

	@Override
	protected Symbol[] production(NonTerminal nonTerminal) {
		tkMean = new NonTerminal("N");
		tkDp = new NonTerminal("N");
		tkStart = new NonTerminal("N");
		tkEnd = new NonTerminal("N");
		tkId = new Terminal(InabitTokenClass.ID);
		Symbol[] symbols = {
				new Terminal(InabitTokenClass.DEFINE),
				new Terminal(InabitTokenClass.OPEN_SQUARE_BRACKETS),
				new Terminal(InabitTokenClass.BELLSHAPED),
				new Terminal(InabitTokenClass.OPEN_PARENTHESIS),
				tkMean,
				new Terminal(InabitTokenClass.SEMI_COLON),
				tkDp,
				new Terminal(InabitTokenClass.CLOSE_PARENTHESIS),
				new Terminal(InabitTokenClass.FROM),
				tkStart,
				new Terminal(InabitTokenClass.TO),
				tkEnd,
				new Terminal(InabitTokenClass.CLOSE_SQUARE_BRACKETS),
				new Terminal(InabitTokenClass.AS),
				tkId,
				new Terminal(InabitTokenClass.SEMI_COLON),
				new InabitSemanticAction(nonTerminal) {
					
					@Override
					public void execute() throws Exception {
						double mean = Double.parseDouble(tkMean.code);
						double dp = Double.parseDouble(tkDp.code);
						double xi = Double.parseDouble(tkStart.code);
						double xf = Double.parseDouble(tkEnd.code);
						String name = tkId.getValue();
						
						FuzzySet set = new FuzzySet(name, xi, xf);
						set.setupFunction(new GaussianFunction(mean, dp));
						
						addFuzzySet(set);
					}
				},
				new NonTerminal("Fs''")
				
		};
		return symbols;
	}

}
